/**
 * Created by liyongliang on 15/9/7.
 */
var initShareWX = function(callback) {
    console.log(1);
    $.ajax({
        data: {
            parameters: '{common:{},data:{url:"www.one-dao.com"}}'
        },
        url: 'http://www.one-dao.com/api/wechat/getTicket?callback=?',
        success: function(json){
            callback && callback(json);
        },
        error: function(xhr) {}
    })
};

function setConfig(appId, timestamp, nonceStr, signature) {
    wx.config({
        debug: false,
        appId: appId,
        timestamp: timestamp,
        nonceStr: nonceStr,
        signature: signature,
        jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage'] // 功能列表，我们要使用JS-SDK的什么功能
    });
}

function setShare() {
    if(window.wx) {
        wx.ready(function(){
            var wxCfg = {
                title: '一道走疯狂旅行家，赢5万元旅游基金！',
                desc: '现在创建主题行程，赢5万元旅游基金！一道走带你体验最深度的旅行方式，疯狂玩转五大洲！',
                link: 'http://www.one-dao.com/html/crazytraveler/index.html',
                imgUrl: 'http://img.one-dao.com/image/h5/crazytraveler.png'
            };
            wx.onMenuShareTimeline(wxCfg);
            wx.onMenuShareAppMessage(wxCfg);
        });
    }
}

initShareWX(function(json) {
    var data;
    if(json.code === 1) {
        data = json.data;
        if(data) {
            setConfig(data.appId, data.timestamp, data.nonceStr, data.signature);
            setShare();
        }
    } else {}
});